package kz.pdt12.hw.tests;

import kz.pdt12.hw.data.GroupData;
import kz.pdt12.hw.utils.SortedListOf;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static kz.pdt12.hw.tests.DataGenerator.*;

/**
 * Created with IntelliJ IDEA.
 * User: raimbek
 * Date: 06.03.13
 * Time: 22:58
 */
public class GroupCreationTests extends TestBase {

    @DataProvider
    public Iterator<Object[]> groupsFromCsvFile() throws IOException, InstantiationException, IllegalAccessException {
        return wrapObjectsForDataProvider(loadObjectsFromCsvFile(new File("groups.txt"), GroupData.class)).iterator();
    }

    @DataProvider
    public Iterator<Object[]> groupsFromXmlFile() throws IOException, InstantiationException, IllegalAccessException {
        return wrapObjectsForDataProvider(loadObjectsFromXmlFile(new File("groups.xml"))).iterator();
    }

    @Test(dataProvider = "groupsFromXmlFile")
    public void testGroupCreationWithValidData(GroupData group) {
        // save old state
        SortedListOf<GroupData> oldList = app.getModel().getGroups();

        // actions
        app.getGroupHelper().createGroup(group);

        // save new state
        SortedListOf<GroupData> newList = app.getModel().getGroups();

        //compare states
        assertThat(newList, equalTo(oldList));

        if (wantToCheck()) {
            if (app.getPropertyKey("check.db").equals("yes")) {
                assertThat(app.getModel().getGroups(), equalTo(app.getHibernateHelper().listGroups()));
            }
            if (app.getPropertyKey("check.ui").equals("yes")) {
                assertThat(app.getModel().getGroups(), equalTo(app.getGroupHelper().getUiGroups()));
            }
        }
    }
}

